<script lang="ts" setup>
import useStore from "@/stores";
import { ref, watchEffect } from "vue";
import { useRoute } from "vue-router";
import GoodsImage from "./components/goods-image.vue";
import GoodsSales from "./components/goods-sales.vue";
import GoodsName from "./components/goods-name.vue";
import GoodsSku from "./components/goods-sku.vue";
import GoodsDetail from "./components/goods-detail.vue";
import GoodsHot from "./components/goods-hot.vue";
import Message from "@/components/message";
import type { CartItem } from "@/types/data";

const { goods, cart } = useStore();
const route = useRoute();

watchEffect(() => {
  goods.getGoodsInfo(route.params.id as string);
});

const currentSkuId = ref("");
const changeSku = (skuId: string) => {
  currentSkuId.value = skuId;
  const sku = goods.goodsInfo.skus.find((item) => item.id === skuId);
  if (sku) {
    goods.goodsInfo.inventory = sku.inventory; //更新库存
    goods.goodsInfo.price = sku.price; //更新价格
    goods.goodsInfo.oldPrice = sku.oldPrice; //更新老价格
  }
};

const number = ref(1);

// 添加sku到购物车
const addCart = async () => {
  // console.log("添加购物车");
  if (!currentSkuId.value) {
    return Message.warning("请选择完整的商品规格");
  }

  // 添加购物车时要考虑登录与未登录
  // 登录: 传skuId与count 服务器 服务器再返回购物车的完整商品信息
  // 未登录： 传skuId与count到pinia中时，数据信息不完整
  // 结论：登录与未登录都传递一个完整数据

  // console.log(goods.goodsInfo);
  // console.log(currentSkuId);
  const sku = goods.goodsInfo.skus.find((item) => {
    return item.id === currentSkuId.value;
  });
  let attrsText = "";
  sku?.specs.forEach((item) => {
    attrsText += item.name + ":" + item.valueName;
  });

  await cart.addCart({
    // 本地添加
    id: goods.goodsInfo.id,
    name: goods.goodsInfo.name,
    picture: goods.goodsInfo.mainPictures[0],
    price: goods.goodsInfo.price,
    count: number.value,
    skuId: currentSkuId.value,
    attrsText,
    selected: true,
    nowPrice: goods.goodsInfo.price,
    stock: goods.goodsInfo.inventory,
    isEffective: true,
  } as CartItem);
  Message.success("添加购物车成功");
};
</script>
<template>
  <div class="seal-goods-page" v-if="goods.goodsInfo.id">
    <div class="container">
      <!-- 面包屑 -->
      <SealBread>
        <SealBreadItem to="/">首页</SealBreadItem>
        <SealBreadItem :to="`/category/${goods.goodsInfo.categories[1].id}`">{{
          goods.goodsInfo.categories[1].name
        }}</SealBreadItem>
        <SealBreadItem
          :to="`/category/sub/${goods.goodsInfo.categories[0].id}`"
          >{{ goods.goodsInfo.categories[0].name }}</SealBreadItem
        >
        <SealBreadItem>{{ goods.goodsInfo.name }}</SealBreadItem>
      </SealBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 1.商品图片 -->
        <div class="media">
          <GoodsImage :goods="goods.goodsInfo"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <GoodsName :goods="goods.goodsInfo"></GoodsName>
          <!-- sku -->
          <GoodsSku :goods="goods.goodsInfo" @changeSku="changeSku"></GoodsSku>
          <!-- 数量 -->
          <SealNumbox v-model="number"></SealNumbox>
          <!--  <SealNumbox :modelValue="number" @update:modelValue="number=$event"></SealNumbox> -->
          <!-- 按钮 -->
          <SealButton type="primary" style="margin-top: 20px" @click="addCart"
            >加入购物车</SealButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="goods.goodsInfo"></GoodsDetail>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
